﻿$(window).load(function () { initLoaded(); });


$(document).ready(function () {

    $(".simple_tip_menu_header_more").simpletip({
        fixed: true,
        position: 'bottom',
        content: '<ul class="menu_more_header"><li class="first"><a rel="nofollow" href="/quy-dinh-dang-tin">Quy định đăng tin</a></li><li><a rel="nofollow" href="/quy-che-thanh-vien">Quy chế thành viên</a></li><li><a rel="nofollow" href="/sitemap">Sitemap</a></li></ul>'
    });

    $("#shareLink").click(function () {
        if ($("#shareForm").is(":hidden")) {
            $("#shareForm").slideDown("slow");
        }
        else {
            $("#shareForm").slideUp("slow");
        }
    });
    

});

var maps_home, geocoder, marker, infowindow;

function initialize_maps(clat, long, content) {

    var latlng = new google.maps.LatLng(clat, long);
    var settings = {
        zoom: 15,
        center: latlng,
        mapTypeControl: true,
        mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU },
        navigationControl: true,
        navigationControlOptions: { style: google.maps.NavigationControlStyle.SMALL },
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    maps_home = new google.maps.Map(document.getElementById("maps_company"), settings);
    
    marker = new google.maps.Marker({
        position: latlng,
        map: maps_home,
        draggable: true
    });

    google.maps.event.addListener(marker, 'dragstart', function () {
        //updateMarkerAddress('Dragging...');
        hideInfoWindow();
    });

    google.maps.event.addListener(marker, 'drag', function () {
        //updateMarkerStatus('Dragging...');
        updatePosition(marker.getPosition());
    });

    google.maps.event.addListener(marker, 'dragend', function () {
        //updateMarkerStatus('Drag ended');
        if (!marker) {
            marker = new google.maps.Marker({
                map: maps_home
            });
            marker.setPosition(marker.getPosition());
        }


        geocodePosition(marker.getPosition());

        

        updateInfoWindow($("#Des_on_Maps").val());



    });
}

function initialize_maps_details(clat, long, content) {

    var latlng = new google.maps.LatLng(clat, long);
    var settings = {
        zoom: 15,
        center: latlng,
        mapTypeControl: true,
        mapTypeControlOptions: { style: google.maps.MapTypeControlStyle.DROPDOWN_MENU },
        navigationControl: true,
        navigationControlOptions: { style: google.maps.NavigationControlStyle.SMALL },
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    maps_home = new google.maps.Map(document.getElementById("maps_company"), settings);

    marker = new google.maps.Marker({
        position: latlng,
        map: maps_home,
        draggable: false
    });

   if (!infowindow) {
        // Creating a new InfoWindow
        infowindow = new google.maps.InfoWindow();
    }

    var contentHTML = '<div id="info_maps" style="width:400px;">' +
						    '<p>' + content + '</p>' +
					    '</div>';

    infowindow.setContent(contentHTML);

    infowindow.open(maps_home, marker);
  
}

function updatePosition(latLng) {
    $("#clatId").val(latLng.lat().toFixed(6));
    $("#clongId").val(latLng.lng().toFixed(6));
}


function getByAddress(address) {

    if (!geocoder) {
        geocoder = new google.maps.Geocoder();
    }

    var geocoderRequest =
	{
	    address: address,
	    'region': 'vn'
	}

    // Making the Geocode request
    geocoder.geocode(geocoderRequest, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            maps_home.setCenter(results[0].geometry.location);

            if (!marker) {
                marker = new google.maps.Marker({
                    map: maps_home
                });
            }

            marker.setPosition(results[0].geometry.location);

            geocodePosition(marker.getPosition());

            updatePosition(results[0].geometry.location);

            maps_home.setZoom(15);

            //var content = results[0].formatted_address;

            //updateInfoWindow(content);

            //updateAddress(results[0].formatted_address);
        }
    });
}

function geocodePosition(pos) {
    if (!geocoder) {
        geocoder = new google.maps.Geocoder();
    }

    geocoder.geocode({ latLng: pos }, function (responses) {
        if (responses && responses.length > 0) {
            var content = responses[0].formatted_address;

            //update infowindow
            maps_home.setCenter(pos);

            //updateInfoWindow(content);
        } else {
            //alert('Cannot determine address at this location.');
        }
    });
}

function validateEmail(email) {
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}

function updateInfoWindow(content) {
    // Check to see if we've already got an InfoWindow object
    if (!infowindow) {
        // Creating a new InfoWindow
        infowindow = new google.maps.InfoWindow();
    }

    var contentHTML = '<div id="info_maps" style="width:400px;">' +
						    '<p>' + content + '</p>' +
					    '</div>';

    infowindow.setContent(contentHTML);

    infowindow.open(maps_home, marker);

    return false;
}

/**
 * hide infowindow
 */
function hideInfoWindow() {
    // Check to see if we've already got an InfoWindow object
    if (!infowindow) {
        // Creating a new InfoWindow
        infowindow = new google.maps.InfoWindow();
    }

    infowindow.close();
}

function loadPartiview(elementSelector, urls) {
    var $this = $(this);
    urls = urls.replace(/&amp;/g, '&');
    $.ajax({
        url: urls,
        type: 'GET',
        dataType: 'html', // <-- to expect an html response
        success: function (result) {
            jQuery("" + elementSelector + "").html(result);
        }
    });

    return false;
}